package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityComDockApi;
import com.ocom.common.entity.company.EntityDockKey;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.company.dao.IDaoDockKey;
import com.ocom.company.service.DockKeyService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

@Service
public class DockKeyServiceImpl extends ServiceImpl<IDaoDockKey, EntityDockKey> implements DockKeyService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityDockKey getDockKeyInfo(String dockCode, Integer grade, Long comId, String serverCode) {
		QueryWrapper<EntityDockKey> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dock_code", dockCode);
		if (grade == 0) {
			queryWrapper.eq("com_id", comId);
		}
		if (grade == 10) {
			queryWrapper.eq("server_code", serverCode);
		}
		queryWrapper.last("limit 1");

		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public boolean removeDockKeyByComIdAndDockCodeAndGrade(Long comId, String dockCode, Integer grade) {
		QueryWrapper<EntityDockKey> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("dock_code", dockCode);
		queryWrapper.eq("grade", grade);
		return this.remove(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityDockKey getDockKeyByGrade(String dockCode, Integer grade, Long comId, String serverCode) {
		QueryWrapper<EntityDockKey> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dock_code", dockCode);
		queryWrapper.eq("grade", grade);
		if (grade == 0) {
			queryWrapper.eq("com_id", comId);
		}
		if (grade == 10) {
			queryWrapper.eq("server_code", serverCode);
		}
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityDockKey getDockKeyInfoReturnNew(String dockCode, Integer grade, Long comId, String serverCode) {
		EntityDockKey entityDockKey = getDockKeyByGrade(dockCode, grade, comId, serverCode);
		if (null == entityDockKey) {
			entityDockKey = new EntityDockKey();
		}
		return entityDockKey;
	}


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public boolean saveDockKey(EntityDockKey entityDockKey) {
		EntityDockKey entity = getDockKeyByGrade(entityDockKey.getDockCode(), entityDockKey.getGrade(), entityDockKey.getComId(),entityDockKey.getServerCode());
		if (null != entity) {
			entity.setMd5Key(entityDockKey.getMd5Key());
			entity.setRsaPri(entityDockKey.getRsaPri());
			entity.setRsaPub(entityDockKey.getRsaPub());
			entity.setRsa2Pri(entityDockKey.getRsa2Pri());
			entity.setRsa2Pub(entityDockKey.getRsa2Pub());
			entity.setSm3Key(entityDockKey.getSm3Key());
			entity.setAesKey(entityDockKey.getAesKey());
		} else {
			entity = entityDockKey;
		}
		return this.saveOrUpdate(entity);
	}


}
